// pages/day/day.js
var util = require('../../utils/util.js')
var app = getApp();
var key = app.globalData.key;
var resurl = app.globalData.resurl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    adverShow: true,
    monthInGanZhi: '',
    yearShengXiao: '',
    yearInGanZhi: '',
    season: '',
    dayYi: '',
    dayJi: '',
    lunar: '',
    day: '',
    month: 'month',
    year: 'year',
    money: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var now = new Date();
    var day = now.getDate();
    let month = now.getMonth() + 1;
    let year = now.getFullYear();
    this.setData({
      day,
      month,
      year
    })
  },
  // 关闭banner广告
  adClose: function () {
    this.setData({
      adverShow: false,
      // videoShow: true,
    })
  },
  getCalendarData(e) { // 监听日历数据
    console.log(e.detail);
    var that = this;
    var year = e.detail.currentYear;
    var month = e.detail.currentMonth;
    var date = e.detail.nowDate;
    var calendar = year + ',' + month + ',' + date;
    console.log(calendar);
    that.getUserInfo(calendar);

  },
  getUserInfo: function (calendar) {
    var that = this;
    that.setData({
      money: app.globalData.money,
    })

    var userId = wx.getStorageSync("userId");
    var _data = {
      deviceProperties: app.globalData.deviceProperties,
      user: {
        userId: userId,
        createTime: calendar,
      }
    }
    _data = JSON.stringify(_data);
    _data = util.base64_encode(_data);
    _data = util.xxtea_encrypt(_data, key);
    //获取用户信息接口
    wx.request({
      url: resurl + '/interface/sdkData?requestId=3&a=1&b=2',
      data: _data,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        // console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          // console.log(res);
          that.setData({
            monthInGanZhi: res.monthInGanZhi,
            yearShengXiao: res.yearShengXiao,
            yearInGanZhi: res.yearInGanZhi,
            season: res.season,
            dayYi: res.dayYi,
            dayJi: res.dayJi,
            lunar: res.lunar,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var money = that.data.money;
    that.setData({
      adverShow: true,
    })

    //日历 接口13
    var userId = wx.getStorageSync("userId");
    var data13 = {
      deviceProperties: app.globalData.deviceProperties,
      user: {
        userId: userId,
        jobId: 4,
      }
    }
    data13 = JSON.stringify(data13);
    data13 = util.base64_encode(data13);
    data13 = util.xxtea_encrypt(data13, key);
    wx.request({
      url: resurl + '/interface/sdkData?requestId=13&a=1&b=2',
      data: data13,
      header: {
        "content-type": "application/json",
        "dataType": "json",
      },
      method: "post",
      success: function (res) {
        res = util.xxtea_decrypt(res.data, key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        console.log(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          if (res.getMoney) {
            app.globalData.momey = money + res.getMoney;
            that.setData({
              money: app.globalData.momey,
            })
          }

        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})